﻿using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using YNCNumberSys.db;


namespace YNCNumberSys.DbDal
{
    
    public class TempFolderDal
    {
        private CommandManager commandManager = new CommandManager();
        private ConnectionManager connectionManager = new ConnectionManager();
        /// <summary>
        /// 将图纸集信息插入表中，后面的排序能用
        /// </summary>
        /// <param name="dt"></param>
        /// <returns></returns>
        public DataTable InsertTempFolder(DataTable dt)
        {
            DataTable dtRtun = null;
            string sqlAll = string.Empty;
            for (int i=0;i< dt.Rows.Count;i++)
            {
                string sNo = (string)dt.Rows[i]["图纸序号"];
                string sSeries = (string)dt.Rows[i]["图纸类型"];
                string sFiles = (string)dt.Rows[i]["原文件名"];
                string sExtend = (string)dt.Rows[i]["扩展名"];
                string sql = string.Format(@"
                   INSERT INTO 
                   [YNCNumberSys].[dbo].[FolderTemp]
                   (
                    [No]
                   ,[series]
                   ,[files]
                   ,[extend]
                    ) 
                   VALUES('{0}','{1}','{2}','{3}');", sNo, sSeries, sFiles, sExtend);
                sqlAll= sqlAll+" "+ sql+" ";
            }
            if (!string.IsNullOrEmpty(sqlAll.Trim()))
            {
                dtRtun = commandManager.ExecuteNonQuery(connectionManager.getConnection(), sqlAll, "FolderTemp");
            }
            return dtRtun;
        }
        public DataTable Build4CNoDwgTable()
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("图纸序号", typeof(string));
            dt.Columns.Add("图纸大小", typeof(string));
            dt.Columns.Add("图纸类型", typeof(string));
            dt.Columns.Add("原文件名", typeof(string));
            dt.Columns.Add("扩展名", typeof(string));

            DataRow dr = dt.NewRow();
            dr["原文件名"] = "";
            dr["扩展名"] = "";
            dr["图纸大小"] = 4;
            dr["图纸类型"] = "4C";
            dr["图纸序号"] = "ZP-4C";
            dt.Rows.Add(dr);
            return dt;
        }
        /// <summary>
        /// 统计文件夹下的文件，构成表。
        /// </summary>
        /// <param name="files">文件</param>
        /// <param name="inclueZP">是否包含ZP，true  包含;false 不包含</param>
        /// <param name="errInfo">返回空表，对应的错误值</param>
        /// <returns>返回表。</returns>
        public DataTable FileInfoTable(FileInfo[] files, 
            bool inclueZP,
            ref string errInfo)
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("图纸序号", typeof(string));
            dt.Columns.Add("图纸大小", typeof(string));
            dt.Columns.Add("图纸类型", typeof(string));
            dt.Columns.Add("原文件名", typeof(string));
            dt.Columns.Add("扩展名", typeof(string));
            int n = files.Length;
            if (n == 0)
                errInfo = @"文件夹为空!";
            bool err = false;
            for (int i = 0; i < n; i++)
            {
                string filenameAndExtend = files[i].Name;//0J-1.dwg
                string extend = files[i].Extension.ToLower();//.dwg
                if (extend != @".dwg") continue;
                string filename = Path.GetFileNameWithoutExtension(filenameAndExtend);//0J-1
                string[] s = filename.Split('-');
                if (s.Length >= 2)
                {
                    if(filename.Length>=7)
                    {
                        errInfo = errInfo+" "+filename;
                        err = true;
                        continue;
                    }
                    string paperSerial = s[0].Length>=2? s[0].Substring(0,2):s[0];//0J123456
                    string no = s[1];//1
                    string paperSize = paperSerial.Substring(0, paperSerial.Length - 1);
                    if (!inclueZP)
                    {
                        if (filename.ToUpper().Contains("ZP"))
                        {
                            errInfo = "含有ZP件，错误，请删除后重建";
                            return dt.Clone();//复制表结构
                        }
                        else
                        {
                            DataRow dr = dt.NewRow();
                            dr["原文件名"] = filename;
                            dr["扩展名"] = extend;
                            dr["图纸大小"] = paperSize;
                            dr["图纸类型"] = paperSerial;
                            dr["图纸序号"] = no;
                            dt.Rows.Add(dr);
                            DataRow[] drSearch = dt.Select(string.Format("'原文件名' like '%{0}%'", "ZP"));
                            if (drSearch.Length > 1)
                            {
                                errInfo = "当前文件夹中含有多个装配件,不符合规则约定！";
                                return dt.Clone();//复制表结构
                            }
                        }
                    }
                    else
                    {
                        if (no == "1")
                        {
                            if (!inclueZP)
                            {
                                errInfo = "文件序号1请删除或者重命名";
                                return dt.Clone();//复制表结构
                            }
                            else
                            {
                                errInfo = "文件序号1请删除或者重命名";
                                return dt.Clone();//复制表结构 
                            }
                        }
                        else
                        {
                            DataRow dr = dt.NewRow();
                            dr["原文件名"] = filename;
                            dr["扩展名"] = extend;
                            dr["图纸大小"] = paperSize;
                            dr["图纸类型"] = paperSerial;
                            dr["图纸序号"] = no;
                            dt.Rows.Add(dr);
                            DataRow[] drSearch = dt.Select(string.Format("'原文件名' like '%{0}%'", "ZP"));
                            if (drSearch.Length > 1)
                            {
                                errInfo = "当前文件夹中含有多个装配件,不符合规则约定！";
                                return dt.Clone();//复制表结构
                            }
                        }
                    }
                }
            }
            if (err)
                errInfo = errInfo + " 可能已经分配过，请改名”**（2位，比如0J）-（键号）.dwg“后再申请";
            return dt;
        }

        public DataTable DeleteRow(DataTable dt,string s图纸类型,string s图纸序号)
        {
            DataTable newdt=dt.Clone();
            for (int i=0;i< dt.Rows.Count;i++)
            {
                 if(dt.Rows[i]["图纸类型"].ToString()!= s图纸类型
                    && dt.Rows[i]["图纸序号"].ToString() != s图纸序号)
                {
                    newdt.Rows.Add(dt.Rows[i].ItemArray);
                }
            }
            return newdt;
        } 
        public DataTable StaticInfoTable(DataTable filesTable)
        {
            DataTable dt = new DataTable();
            dt.Columns.Add(@"图纸数量", typeof(int));
            dt.Columns.Add(@"图纸大小",typeof(string));
            dt.Columns.Add(@"图纸类型", typeof(string));
            DataRow[] drSearch=filesTable.Select("1=1", "图纸类型 ASC,图纸大小 ASC");
            int num = 0;
            string paperSize = "";
            string paperSerial = "";
            for (int i=0;i< drSearch.Length;i++)
            {
                if(paperSerial != (string)(drSearch[i]["图纸类型"]))
                {
                    if(!string.IsNullOrEmpty(paperSerial))
                    {
                        DataRow dr = dt.NewRow();
                        dr["图纸数量"] = num;
                        dr["图纸大小"] = paperSize;
                        dr["图纸类型"] = paperSerial;
                        dt.Rows.Add(dr);
                    }
                    paperSerial = (string)drSearch[i]["图纸类型"];
                    num = 1;
                    paperSize = (string)drSearch[i]["图纸大小"];
                    if (i + 1 == drSearch.Length)
                    {
                        DataRow dr = dt.NewRow();
                        dr["图纸数量"] = num;
                        dr["图纸大小"] = paperSize;
                        dr["图纸类型"] = paperSerial;
                        dt.Rows.Add(dr);
                    }
                }
                else
                {
                    if(paperSize != (string)drSearch[i]["图纸大小"])
                    {
                        {
                            DataRow dr = dt.NewRow();
                            dr["图纸数量"] = num;
                            dr["图纸大小"] = paperSize;
                            dr["图纸类型"] = paperSerial;
                            dt.Rows.Add(dr);
                        }
                        num = 1;
                        paperSize = (string)drSearch[i]["图纸大小"];
                        if (i + 1 == drSearch.Length)
                        {
                            DataRow dr = dt.NewRow();
                            dr["图纸数量"] = num;
                            dr["图纸大小"] = paperSize;
                            dr["图纸类型"] = paperSerial;
                            dt.Rows.Add(dr);
                        }
                    }
                    else
                    {
                        num = num+1;
                        if(i+1== drSearch.Length)
                        {
                            DataRow dr = dt.NewRow();
                            dr["图纸数量"] = num;
                            dr["图纸大小"] = paperSize;
                            dr["图纸类型"] = paperSerial;
                            dt.Rows.Add(dr);
                        }
                    }
                }
            }
            return dt;
        }
    }
}
